#!/bin/bash
#
# OKX 交易机器人 Docker 快速启动脚本
# 

set -e

echo "========================================"
echo "🤖 OKX 交易机器人 Docker 部署"
echo "========================================"
echo ""

# 检查 Docker 是否安装
if ! command -v docker &> /dev/null; then
    echo "❌ 错误：未检测到 Docker，请先安装 Docker"
    echo "   安装指南：https://docs.docker.com/get-docker/"
    exit 1
fi

# 检查 Docker Compose 是否安装
if ! command -v docker-compose &> /dev/null; then
    echo "❌ 错误：未检测到 Docker Compose，请先安装"
    echo "   安装指南：https://docs.docker.com/compose/install/"
    exit 1
fi

echo "✅ Docker 环境检查通过"
echo ""

# 检查 .env 文件
if [ ! -f .env ]; then
    echo "⚠️  未找到 .env 文件"
    echo "   正在创建 .env 模板..."
    
    cat > .env << 'EOF'
# 交易模式：demo=模拟盘, live=实盘
TRADE_MODE=demo

# DeepSeek AI API
DEEPSEEK_API_KEY=your_deepseek_api_key

# OKX 实盘 API
OKX_API_KEY=your_okx_api_key
OKX_SECRET=your_okx_secret
OKX_PASSWORD=your_okx_password

# OKX 模拟盘 API
OKX_DEMO_API_KEY=your_okx_demo_api_key
OKX_DEMO_SECRET=your_okx_demo_secret
OKX_DEMO_PASSWORD=your_okx_demo_password

# Telegram 通知（可选）
TELEGRAM_BOT_TOKEN=
TELEGRAM_CHAT_ID=
EOF
    
    echo "✅ .env 文件已创建"
    echo ""
    echo "⚠️  请编辑 .env 文件，填入你的 API 密钥："
    echo "   nano .env"
    echo "   或"
    echo "   vim .env"
    echo ""
    read -p "配置完成后按 Enter 继续..."
fi

# 读取配置
source .env

# 检查必要的配置
if [ -z "$DEEPSEEK_API_KEY" ] || [ "$DEEPSEEK_API_KEY" = "your_deepseek_api_key" ]; then
    echo "❌ 错误：请在 .env 文件中配置 DEEPSEEK_API_KEY"
    exit 1
fi

if [ "$TRADE_MODE" = "demo" ]; then
    if [ -z "$OKX_DEMO_API_KEY" ] || [ "$OKX_DEMO_API_KEY" = "your_okx_demo_api_key" ]; then
        echo "⚠️  警告：模拟盘模式需要配置 OKX_DEMO_API_KEY"
        echo "   如果未配置，将尝试使用实盘 API Key"
    fi
elif [ "$TRADE_MODE" = "live" ]; then
    if [ -z "$OKX_API_KEY" ] || [ "$OKX_API_KEY" = "your_okx_api_key" ]; then
        echo "❌ 错误：实盘模式必须配置 OKX_API_KEY"
        exit 1
    fi
    echo "⚠️⚠️⚠️ 警告：你正在使用实盘模式！⚠️⚠️⚠️"
    echo "   这将使用真实资金进行交易"
    read -p "确认继续？(输入 YES): " confirm
    if [ "$confirm" != "YES" ]; then
        echo "已取消"
        exit 0
    fi
fi

echo ""
echo "📊 配置信息："
echo "   交易模式: $TRADE_MODE"
echo "   DeepSeek API: ${DEEPSEEK_API_KEY:0:10}..."
if [ "$TRADE_MODE" = "demo" ]; then
    echo "   OKX 模拟盘 API: ${OKX_DEMO_API_KEY:0:10}..."
else
    echo "   OKX 实盘 API: ${OKX_API_KEY:0:10}..."
fi
echo ""

# 停止旧容器
if [ "$(docker ps -a -q -f name=okx-trade-bot)" ]; then
    echo "停止旧容器..."
    docker-compose down
fi

# 构建并启动
echo "构建并启动容器..."
docker-compose up -d --build

echo ""
echo "========================================"
echo "✅ 部署完成！"
echo "========================================"
echo ""
echo "📋 常用命令："
echo "   查看运行状态:  docker-compose ps"
echo "   查看实时日志:  docker-compose logs -f"
echo "   查看日志文件:  tail -f logs/trade_bot.log"
echo "   停止程序:      docker-compose stop"
echo "   重启程序:      docker-compose restart"
echo "   删除容器:      docker-compose down"
echo ""
echo "📝 日志文件位置:"
echo "   主日志:  logs/trade_bot.log"
echo "   错误日志: logs/error.log"
echo "   交易日志: logs/trade.log"
echo ""

